package Hxw3DEngine.proxy
{
	import Hxw3DEngine.error.SingleError;
	import Hxw3DEngine.utils.AGALMiniAssembler;
	
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Program3D;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	public class Program3DProxy
	{
		private var program3ds:Dictionary;
		
		private var _stage3dProxy:Stage3DProxy;
		
		public function Program3DProxy()
		{
			if(_instance) throw new SingleError();
			program3ds = new Dictionary;
		}
		
		private static var _instance:Program3DProxy;
		
		public static function get instance():Program3DProxy{
			return _instance ||= new Program3DProxy();
		}
		
		public function creatProgram(vertexCode:String,fragmentCode:String):Program3D{
			var program:Program3D;
			var key:String = getKey(vertexCode,fragmentCode);
			if(!program3ds[key]){
				program = Stage3DProxy.context3d.createProgram();
				var vertexByte:ByteArray = new AGALMiniAssembler().assemble(Context3DProgramType.VERTEX,vertexCode);
				var fragmentByte:ByteArray = new AGALMiniAssembler().assemble(Context3DProgramType.FRAGMENT,fragmentCode);
				program.upload(vertexByte,fragmentByte);
				program3ds[key] = program;
			}
			return program3ds[key];
		}
		
		private function getKey(vertexCode:String,fragmentcode:String):String{
			return vertexCode + "---" + fragmentcode;
		}
		
		
		
	}
}